package com.abhipedro.wordsearch.model.dictionary;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import android.content.Context;
import android.preference.PreferenceManager;
import android.util.Log;

import com.abhipedro.wordsearch.view.R;


/**
 * @author abhi
 * Provides access to words of given language through database.
 */
public class DictionaryManager {

	private final int MAX_TRIES = 5;

	private WordsDbAdapter mWordsDbAdapter;

	private Random mRandom;

	private ArrayList<String> mWords;

	int index = 0;

	public DictionaryManager(Context ctx) {
		mWordsDbAdapter = new WordsDbAdapter(ctx);
		index = 0;

		mWords = new ArrayList<String>();
		
		// set the default value based on locale 
		String defVal = ctx.getString(R.string.ENGLISH);
		
		String localeVal = ctx.getResources().getConfiguration().locale
				.getCountry();
		if (localeVal.compareTo("DE") == 0) {
			defVal = ctx.getString(R.string.GERMAN);
		}
		if (localeVal.compareTo("IT") == 0) {
			defVal = ctx.getString(R.string.ITALIAN);
		}
		if (localeVal.compareTo("FR") == 0) {
			defVal = ctx.getString(R.string.FRENCH);
		}
		if (localeVal.compareTo("ES") == 0) {
			defVal = ctx.getString(R.string.SPANISH);
		}
		if (localeVal.compareTo("PT") == 0) {
			defVal = ctx.getString(R.string.PORTUGUESE);
		}
		if (localeVal.compareTo("SE") == 0) {
			defVal = ctx.getString(R.string.SWEDISH);
		}
		if (localeVal.compareTo("NL") == 0) {
			defVal = ctx.getString(R.string.DUTCH);
		}
		
		String language = PreferenceManager.getDefaultSharedPreferences(ctx)
				.getString(ctx.getString(R.string.prefs_category),	defVal);
		
		Log.e("Chosen langauge is ", language );

		// check if language is numbers? 
		if (language == ctx.getString(R.string.NUMBERS)) {
			mRandom = new Random();

			for (int i = 0; i < 50; i++) {
				mWords.add(getRandomNumbers(4, 9));
			}

		} else {
			
			boolean fail = false;
			try {
				mWordsDbAdapter.open();
				mWords = mWordsDbAdapter.getWordsForLang(language);
			} catch (Exception e ) {
				Log.e("tag , swithcing to fallback", "");
				fail = true;
			}
			
			Log.e("got words", mWords.size() + "");
			
			// fallback to default dictionaries
			if (mWords.size() < 5 || fail== true) {
				
				Log.e("error , swithcing to fallback", "");
				String[] tmp = null;
				if(language.compareTo( ctx.getString(R.string.GERMAN))==0){
					tmp = ctx.getResources().getStringArray(R.array.german_words);
				}
				if(language.compareTo( ctx.getString(R.string.FRENCH))==0){
					tmp = ctx.getResources().getStringArray(R.array.french_words);
				}
				if(language.compareTo( ctx.getString(R.string.PORTUGUESE))==0){
					tmp = ctx.getResources().getStringArray(R.array.portuguese_words);
				}
				if(language.compareTo( ctx.getString(R.string.SPANISH))==0){
					tmp = ctx.getResources().getStringArray(R.array.spanish_words);
				}
				if(language.compareTo( ctx.getString(R.string.ITALIAN))==0){
					tmp = ctx.getResources().getStringArray(R.array.italian_words);
				}
				if(language.compareTo(ctx.getString(R.string.SWEDISH))==0){
					tmp = ctx.getResources().getStringArray(R.array.swedish_words);
				}
				if(language.compareTo( ctx.getString(R.string.DUTCH))==0){
					tmp = ctx.getResources().getStringArray(R.array.dutch_words);
				}
				else 
					tmp = ctx.getResources().getStringArray(R.array.english_words);
				
				for (String s : tmp)
					mWords.add(s);
				Collections.shuffle(mWords);
			}
			
			mWordsDbAdapter.close();
		}

	}


	public String getNextWord() {
		int tries = 0;
		String str = "";

		try {
			do {
				str = mWords.get(index++);
				tries++;
			} while ((str.length() < 3 || str.length() > 7)
					&& tries < MAX_TRIES);
			
			str = str.toUpperCase();
		} catch (Exception e) {
		}
		return str;
	}
	
	public String getRandomNumbers(int minLength, int maxLength) {
		String tmp = "";
		int length = minLength;
		length += mRandom.nextInt(maxLength - minLength);
		
		for (int index = 0; index < length; index++) {
			tmp += mRandom.nextInt(10);
		}
		return tmp;
	}

}
